﻿//namespace WebUI.Controls
//{
//    using MgExtensions.TrafficLights;
//    using MgExtensions.TrafficLights.DAL;
//    using System;
//    using System.Collections.Generic;
//    using System.Configuration;
//    using System.Globalization;
//    using System.Linq;
//    using System.Runtime.CompilerServices;
//    using System.Web.UI;
//    using System.Web.UI.HtmlControls;
//    using System.Web.UI.WebControls;

//    public class IncidentReport : UserControl
//    {
//        protected Panel closedIncidentsResultPanel;
//        protected GridView closedMasterGrid;
//        protected Button doFilter;
//        protected TextBox dtFrom;
//        protected TextBox dtTo;
//        protected GridView MasterGrid;
//        protected Panel noResultPanel;
//        protected Panel resultPanel;
//        protected CheckBox showClosedIncidents;

//        private void BindMasterGrid(GridView grid, IEnumerable<TrafficLightsManager.JunctionInc> datasource)
//        {
//            grid.DataSource = datasource;
//            grid.RowCommand += new GridViewCommandEventHandler(this.MasterGrid_RowCommand);
//            grid.RowDataBound += new GridViewRowEventHandler(this.MasterGrid_RowDataBound);
//            grid.DataBind();
//        }

//        protected void DoFilter(object sender, EventArgs e)
//        {
//            this.FilterClosedIncidents(this.TlManager.GetJunctionsWithIncedents());
//        }

//        private void FilterClosedIncidents(IEnumerable<TrafficLightsManager.JunctionInc> junctionsWhthIncidents)
//        {
//            DateTime? dateFrom = this.ParseStringDate(this.dtFrom.Text);
//            DateTime? dateTo = this.ParseStringDate(this.dtTo.Text);
//            IEnumerable<TrafficLightsManager.JunctionInc> junctionsWithClosedIncidents = from x in junctionsWhthIncidents
//                where x.Incident.Status.Level == -1
//                select x;
//            if (dateFrom.HasValue)
//            {
//                junctionsWithClosedIncidents = Enumerable.Where<TrafficLightsManager.JunctionInc>(junctionsWithClosedIncidents, delegate (TrafficLightsManager.JunctionInc x) {
//                    DateTime? CS$0$0001 = x.DtStart;
//                    DateTime? CS$0$0002 = dateFrom;
//                    return (CS$0$0001.HasValue & CS$0$0002.HasValue) ? (CS$0$0001.GetValueOrDefault() > CS$0$0002.GetValueOrDefault()) : false;
//                });
//            }
//            if (dateTo.HasValue)
//            {
//                junctionsWithClosedIncidents = Enumerable.Where<TrafficLightsManager.JunctionInc>(junctionsWithClosedIncidents, delegate (TrafficLightsManager.JunctionInc x) {
//                    DateTime? CS$0$0001 = x.DtStart;
//                    DateTime? CS$0$0002 = dateTo;
//                    return (CS$0$0001.HasValue & CS$0$0002.HasValue) ? (CS$0$0001.GetValueOrDefault() < CS$0$0002.GetValueOrDefault()) : false;
//                });
//            }
//            this.BindMasterGrid(this.closedMasterGrid, junctionsWithClosedIncidents);
//        }

//        protected string FormatIncidentLogStatus(object dataItem)
//        {
//            TrafficLightsManager.JunctionInc incident = dataItem as TrafficLightsManager.JunctionInc;
//            return ((incident != null) ? FormatMinutes(incident.IncidentLogStatus) : string.Empty);
//        }

//        private static string FormatMinutes(int minutes)
//        {
//            TimeSpan timespan = new TimeSpan(0, minutes, 0);
//            return string.Format(ConfigurationManager.AppSettings.get_Item("TimespanDHMFormat"), timespan.Days, timespan.Hours, timespan.Minutes);
//        }

//        protected string FormatStdTime(object dataItem)
//        {
//            TrafficLightsManager.JunctionInc incident = dataItem as TrafficLightsManager.JunctionInc;
//            return ((incident != null) ? FormatMinutes(incident.StdTime.HasValue ? incident.StdTime.Value : 0) : string.Empty);
//        }

//        protected string GetFixText(object dataItem)
//        {
//            TrafficLightsManager.JunctionInc junctionInc = dataItem as TrafficLightsManager.JunctionInc;
//            return ((((junctionInc != null) && (junctionInc.Incident != null)) && (junctionInc.Incident.Fix != null)) ? junctionInc.Incident.Fix.Name : string.Empty);
//        }

//        public object GetIncedentLog(object item)
//        {
//            return (from i in ((TrafficLightsManager.JunctionInc) item).IncidentLog select new { Status = this.TlManager.GetStatusName(i.StatusID), Description = i.Description, StatusSDate = i.StatusSDate, StatusEDate = i.StatusEDate, StdTime = (i.Damage == null) ? string.Empty : (i.StatusEDate.HasValue ? ((int) i.StatusEDate.Value.Subtract(i.StatusSDate).Subtract(new TimeSpan(0, i.Damage.StdTimeMetro.HasValue ? i.Damage.StdTimeMetro.Value : 0, 0)).TotalHours) : ((int) DateTime.Now.Subtract(i.StatusSDate).Subtract(new TimeSpan(0, i.Damage.StdTimeMetro.HasValue ? i.Damage.StdTimeMetro.Value : 0, 0)).TotalHours)).ToString(), Notes = i.Notes }).ToList();
//        }

//        protected string GetPropertyTypeName(object dataItem)
//        {
//            TrafficLightsManager.JunctionInc junctionInc = dataItem as TrafficLightsManager.JunctionInc;
//            return ((((junctionInc != null) && (junctionInc.Incident != null)) && (junctionInc.Incident.PropType != null)) ? junctionInc.Incident.PropType.Name : string.Empty);
//        }

//        protected void MasterGrid_RowCommand(object sender, GridViewCommandEventArgs e)
//        {
//            GridView grid = sender as GridView;
//            if ((grid != null) && !string.IsNullOrEmpty(e.CommandArgument as string))
//            {
//                int rowIdx = int.Parse(e.CommandArgument.ToString());
//                for (int i = 0; i < grid.Rows.Count; i++)
//                {
//                    GridViewRow row = grid.Rows[i];
//                    GridView detailGrid = row.FindControl("DetailGrid") as GridView;
//                    if (detailGrid != null)
//                    {
//                        string controlId = detailGrid.ClientID;
//                        if (i == rowIdx)
//                        {
//                            if (!((this.ViewState[controlId] != null) && ((bool) this.ViewState[controlId])))
//                            {
//                                this.ViewState[controlId] = true;
//                            }
//                            else
//                            {
//                                this.ViewState[controlId] = false;
//                            }
//                        }
//                        if (!((this.ViewState[controlId] != null) && ((bool) this.ViewState[controlId])))
//                        {
//                            this.ViewState[controlId] = false;
//                            detailGrid.Visible = false;
//                        }
//                        else
//                        {
//                            this.ViewState[controlId] = true;
//                            detailGrid.Visible = true;
//                        }
//                    }
//                }
//            }
//        }

//        protected void MasterGrid_RowDataBound(object sender, GridViewRowEventArgs e)
//        {
//            if (e.Row.RowType.Equals(DataControlRowType.DataRow))
//            {
//                string divClass;
//                HtmlContainerControl iconDiv = e.Row.FindControl("IconDiv") as HtmlContainerControl;
//                TrafficLightsManager.JunctionInc junc = (TrafficLightsManager.JunctionInc) e.Row.DataItem;
//                int CS$0$0001 = junc.IncidentLogStatus;
//                if ((CS$0$0001 <= junc.StdTime) && junc.Incident.DtEnd.HasValue)
//                {
//                    divClass = "green";
//                }
//                else
//                {
//                    CS$0$0001 = junc.IncidentLogStatus;
//                    if (!((CS$0$0001 > junc.StdTime) || junc.Incident.DtEnd.HasValue))
//                    {
//                        divClass = "yellow";
//                    }
//                    else
//                    {
//                        divClass = "red";
//                    }
//                }
//                if (iconDiv != null)
//                {
//                    iconDiv.Attributes.Add("class", divClass);
//                }
//            }
//        }

//        protected void Page_Load(object sender, EventArgs e)
//        {
//            if (!base.IsPostBack)
//            {
//                this.dtFrom.Text = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)).ToString("dd/MM/yyyy HH:mm");
//                this.dtTo.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
//            }
//            List<TrafficLightsManager.JunctionInc> junctionsWhthIncidents = this.TlManager.GetJunctionsWithIncedents();
//            IEnumerable<TrafficLightsManager.JunctionInc> junctionsWithOpenedIncedents = from x in junctionsWhthIncidents
//                where x.Incident.Status.Level != -1
//                select x;
//            this.SwitchPanels(junctionsWithOpenedIncedents.Count<TrafficLightsManager.JunctionInc>());
//            this.BindMasterGrid(this.MasterGrid, junctionsWithOpenedIncedents);
//            this.doFilter.Visible = this.showClosedIncidents.Checked;
//            if (this.showClosedIncidents.Checked)
//            {
//                this.FilterClosedIncidents(junctionsWhthIncidents);
//                this.closedIncidentsResultPanel.Visible = true;
//                this.SwitchPanels(junctionsWhthIncidents.Count<TrafficLightsManager.JunctionInc>());
//            }
//            else
//            {
//                this.closedIncidentsResultPanel.Visible = false;
//            }
//            this.Page.DataBind();
//        }

//        private DateTime? ParseStringDate(string str)
//        {
//            DateTime date;
//            if (!(string.IsNullOrEmpty(str) || !DateTime.TryParseExact(str, "dd/MM/yyyy HH:mm", new CultureInfo("he-IL"), DateTimeStyles.None, out date)))
//            {
//                return new DateTime?(date);
//            }
//            return null;
//        }

//        private void SwitchPanels(int resultCount)
//        {
//            this.resultPanel.Visible = resultCount > 0;
//            this.noResultPanel.Visible = resultCount == 0;
//        }

//        public TrafficLightsManager TlManager { get; set; }
//    }
//}

